package com.online.servlet.user;

import com.online.pojo.User;
import com.online.service.user.Impl.UserServiceImpl;
import com.online.service.user.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SubmitUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String user_tel=req.getParameter("user_tel");
        String user_n=req.getParameter("user_n");
        String user_p=req.getParameter("user_p");

        UserService userService= new UserServiceImpl();
        User user =userService.login(user_n);
        if(user.getUser_number()==null){
            boolean fg= userService.submit(user_tel,user_n,user_p);
            if (fg) {
                System.out.println("注册成功！");
                req.getRequestDispatcher("/index.jsp").forward(req,resp);
            }else {
                System.out.println("注册失败");
            }
        }else{
            System.out.println("用户已存在！请换个账号注册！");
            req.setAttribute("info","用户已存在！请换个账号注册！");
            req.getRequestDispatcher("/jsp/Submit.jsp").forward(req,resp);
        }
    }
}
